package com.rjwh.dingdong.client.mvp.presenter.impl;

import android.content.Context;

import com.aishua.lib.task.AsResult;
import com.aishua.lib.utils.AsStrUtils;
import com.rjwh.dingdong.client.bean.jsonbean.ResPublishTopicBean;
import com.rjwh.dingdong.client.bean.localbean.TopicData;
import com.rjwh.dingdong.client.constant.NetConstant;
import com.rjwh.dingdong.client.mvp.BasePresenter;
import com.rjwh.dingdong.client.mvp.model.impl.CommunityPostPublishModel;
import com.rjwh.dingdong.client.mvp.presenter.ICommunityPostPublishPresenter;
import com.rjwh.dingdong.client.mvp.view.CommunityPostPublishView;
import com.rjwh.dingdong.client.util.ToastUtil;

/**
 * @author Kay
 * @ClassName CommunityPostPublishPresenter
 * @Description
 * @date 2016-7-18
 * @history 2016-7-18 author: description:
 */
public class CommunityPostPublishPresenter extends BasePresenter<CommunityPostPublishView> implements ICommunityPostPublishPresenter {

    private CommunityPostPublishModel model;

    public CommunityPostPublishPresenter(Context context, CommunityPostPublishView mMvpView) {
        super(context, mMvpView);
        this.model = new CommunityPostPublishModel(this);
    }

    @Override
    public void doPublishTopic(String modularid, String title, String content, String attachids, String tagids, String address) {
        if (isViewAttached()) {
            getMvpView().showLoading();
            model.doPublishTopic(modularid, title, content, attachids, tagids, address);
        }
    }

    @Override
    public void doPublishReply(String modularid, String topicid, String parentid, String replyid, String title, String content, String attachids, String address) {
        if (isViewAttached()) {
            getMvpView().showLoading();
            model.doPublishReply(modularid, topicid, parentid, replyid, title, content, attachids, address);
        }
    }

    @Override
    public void onServerDataAcquired(int businessType, AsResult mAsResult, boolean isRefresh) {
        if (isViewAttached()) {
            getMvpView().dismissLoading();
            switch (businessType) {
                case NetConstant.COMMUNITY_PUBLISHTOPIC:
                    ResPublishTopicBean topicBean = (ResPublishTopicBean) mAsResult.getObj();
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS && topicBean != null && topicBean.getData() != null) {
                        TopicData topicData = topicBean.getData().getTopic();
                        if (topicData != null) {
                            isViewAttached();
                            getMvpView().LoadTopicData(topicData);
                        }
                    } else {
                        if (!AsStrUtils.isEmpty(mAsResult.getResultMsg())) {
                            ToastUtil.showToast(getContext(), mAsResult.getResultMsg());
                        }
                    }
                    break;
                case NetConstant.COMMUNITY_PUBLISHREPLY:
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS) {
                        String resultMsg = mAsResult.getResultMsg();
                        if (!AsStrUtils.isEmpty(resultMsg)) {
                            getMvpView().LoadReplyData(resultMsg);
                        }
                    } else {
                        if (!AsStrUtils.isEmpty(mAsResult.getResultMsg())) {
                            ToastUtil.showToast(getContext(), mAsResult.getResultMsg());
                        }
                    }
                    break;

                default:
                    break;
            }
        }
    }

    @Override
    public void detachView() {
        model.detachView();
        detachViews();
    }

}
